home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / chardet / sbcharsetprober.py < prev    next >
Text File  |  2006-10-21  |  5KB  |  107 lines

  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is Mozilla Universal charset detector code.
  3. #
  4. # The Initial Developer of the Original Code is
  5. # Netscape Communications Corporation.
  6. # Portions created by the Initial Developer are Copyright (C) 2001
  7. # the Initial Developer. All Rights Reserved.
  8. #
  9. # Contributor(s):
  10. #   Mark Pilgrim - port to Python
  11. #   Shy Shalom - original C code
  12. #
  13. # This library is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU Lesser General Public
  15. # License as published by the Free Software Foundation; either
  16. # version 2.1 of the License, or (at your option) any later version.
  17. # This library is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. # Lesser General Public License for more details.
  21. # You should have received a copy of the GNU Lesser General Public
  22. # License along with this library; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  24. # 02110-1301  USA
  25. ######################### END LICENSE BLOCK #########################
  26.  
  27. import constants, sys
  28. from charsetprober import CharSetProber
  29.  
  30. SAMPLE_SIZE = 64
  31. SB_ENOUGH_REL_THRESHOLD = 1024
  32. POSITIVE_SHORTCUT_THRESHOLD = 0.95
  33. NEGATIVE_SHORTCUT_THRESHOLD = 0.05
  34. SYMBOL_CAT_ORDER = 250
  35. NUMBER_OF_SEQ_CAT = 4
  36. POSITIVE_CAT = NUMBER_OF_SEQ_CAT - 1
  37. #NEGATIVE_CAT = 0
  38.  
  39. class SingleByteCharSetProber(CharSetProber):
  40.     def __init__(self, model, reversed=constants.False, nameProber=None):
  41.         CharSetProber.__init__(self)
  42.         self._mModel = model
  43.         self._mReversed = reversed # TRUE if we need to reverse every pair in the model lookup
  44.         self._mNameProber = nameProber # Optional auxiliary prober for name decision
  45.         self.reset()
  46.  
  47.     def reset(self):
  48.         CharSetProber.reset(self)
  49.         self._mLastOrder = 255 # char order of last character
  50.         self._mSeqCounters = [0] * NUMBER_OF_SEQ_CAT
  51.         self._mTotalSeqs = 0
  52.         self._mTotalChar = 0
  53.         self._mFreqChar = 0 # characters that fall in our sampling range
  54.  
  55.     def get_charset_name(self):
  56.         if self._mNameProber:
  57.             return self._mNameProber.get_charset_name()
  58.         else:
  59.             return self._mModel['charsetName']
  60.  
  61.     def feed(self, aBuf):
  62.         if not self._mModel['keepEnglishLetter']:
  63.             aBuf = self.filter_without_english_letters(aBuf)
  64.         aLen = len(aBuf)
  65.         if not aLen:
  66.             return self.get_state()
  67.         for c in aBuf:
  68.             order = self._mModel['charToOrderMap'][ord(c)]
  69.             if order < SYMBOL_CAT_ORDER:
  70.                 self._mTotalChar += 1
  71.             if order < SAMPLE_SIZE:
  72.                 self._mFreqChar += 1
  73.                 if self._mLastOrder < SAMPLE_SIZE:
  74.                     self._mTotalSeqs += 1
  75.                     if not self._mReversed:
  76.                         self._mSeqCounters[self._mModel['precedenceMatrix'][(self._mLastOrder * SAMPLE_SIZE) + order]] += 1
  77.                     else: # reverse the order of the letters in the lookup
  78.                         self._mSeqCounters[self._mModel['precedenceMatrix'][(order * SAMPLE_SIZE) + self._mLastOrder]] += 1
  79.             self._mLastOrder = order
  80.  
  81.         if self.get_state() == constants.eDetecting:
  82.             if self._mTotalSeqs > SB_ENOUGH_REL_THRESHOLD:
  83.                 cf = self.get_confidence()
  84.                 if cf > POSITIVE_SHORTCUT_THRESHOLD:
  85.                     if constants._debug:
  86.                         sys.stderr.write('%s confidence = %s, we have a winner\n' % (self._mModel['charsetName'], cf))
  87.                     self._mState = constants.eFoundIt
  88.                 elif cf < NEGATIVE_SHORTCUT_THRESHOLD:
  89.                     if constants._debug:
  90.                         sys.stderr.write('%s confidence = %s, below negative shortcut threshhold %s\n' % (self._mModel['charsetName'], cf, NEGATIVE_SHORTCUT_THRESHOLD))
  91.                     self._mState = constants.eNotMe
  92.  
  93.         return self.get_state()
  94.  
  95.     def get_confidence(self):
  96.         r = 0.01
  97.         if self._mTotalSeqs > 0:
  98. #            print self._mSeqCounters[POSITIVE_CAT], self._mTotalSeqs, self._mModel['mTypicalPositiveRatio']
  99.             r = (1.0 * self._mSeqCounters[POSITIVE_CAT]) / self._mTotalSeqs / self._mModel['mTypicalPositiveRatio']
  100. #            print r, self._mFreqChar, self._mTotalChar
  101.             r = r * self._mFreqChar / self._mTotalChar
  102.             if r >= 1.0:
  103.                 r = 0.99
  104.         return r
  105.